home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / StepPlatform Demo ƒ / StepPlatform.c < prev    next >
Text File  |  1996-05-11  |  7KB  |  245 lines

  1. // StepPlatform by Nissan Zafrir, based on MyPlatform and StepZkrolly
  2.  
  3. // Modifications by Ingemar R:
  4. // Scroll-length timed after TickCount in order to make it fast enough on slow Macs
  5. // Replaced ox/oy by gSAT.wind.port->portRect.left and .top (ox/oy are obsolete)
  6. // B/W icons and patterns
  7. // Centered window
  8. // Aligned scrolling to multiples of 8 (to account for a limitation in the 1-bit and 4-bit blitters)
  9.  
  10.  
  11. /*
  12. Desirable improvements:
  13. • The man should look better
  14. • Some kind of opponents?
  15. • Ladders?
  16. • Better, more general rect-bounce utilities?
  17. */
  18.  
  19.  
  20.  
  21. //• main;
  22. #include <SAT.h>
  23. #include "myPlatform.h"
  24.  
  25. SATPatHandle    thepat;
  26. SpritePtr        ignoreSp;
  27. Point            p;
  28. SpritePtr        playerSp;
  29. WindowPtr        gWind;
  30. short            gVBLInstalled;
  31. Rect             r;
  32.  
  33.  
  34. enum {
  35.     scrollSizeH = 512,
  36.     scrollSizeV = 384
  37. };
  38.  
  39. Point nowOff = {0,0};
  40.  
  41. #define max(x, y)    (x>y?x:y)
  42. #define min(x, y)    (x>y?y:x)
  43. #define abs(x)        ((x)>0?(x):-(x))
  44.  
  45. pascal Boolean ScrollScreen()
  46. {
  47.     #define    sco    8    //the speed of the scroll, number of pixels per *tick*
  48.  
  49.     long startTicks, frameTime = 1;
  50.  
  51.     Rect srcRect;
  52.     Point    where,were;
  53.  
  54. // If the player sprite is at the border, scroll!
  55.     if ((playerSp->position.h + 64 > scrollSizeH + gSAT.wind.port->portRect.left) ||
  56.         (playerSp->position.h < gSAT.wind.port->portRect.left) || 
  57.         (playerSp->position.v + 64 > scrollSizeV + gSAT.wind.port->portRect.top) ||
  58.         (playerSp->position.v  < gSAT.wind.port->portRect.top) )
  59. //gSAT.wind.port->portRect.left/top = ox/oy!
  60.     {
  61.         were = nowOff;
  62.         where = playerSp->position;
  63.         where.h -= (scrollSizeH >> 1);
  64.         where.v -= (scrollSizeV >> 1);
  65.         if (where.h < 0)
  66.             where.h = 0;
  67.         if (where.v < 0)
  68.             where.v = 0;
  69.         if (where.h + scrollSizeH > gSAT.offSizeH)
  70.             where.h = gSAT.offSizeH - scrollSizeH;
  71.         if (where.v + scrollSizeV > gSAT.offSizeV)
  72.             where.v = gSAT.offSizeV - scrollSizeV;
  73.  
  74.         where.h &= 0xfff8; // Scroll only to multiples of 8, so we won't confuse the 4-bit and 1-bit blitters!
  75.  
  76.         do
  77.         {
  78.             startTicks = TickCount();
  79.  
  80.             if (nowOff.h > where.h) 
  81.                 nowOff.h = max(nowOff.h - sco*frameTime, where.h);
  82.             if (nowOff.h < where.h) 
  83.                 nowOff.h = min(nowOff.h + sco*frameTime, where.h);
  84.             if (nowOff.v > where.v) 
  85.                 nowOff.v = max(nowOff.v - sco*frameTime, where.v);
  86.             if (nowOff.v < where.v) 
  87.                 nowOff.v = min(nowOff.v + sco*frameTime, where.v);
  88.             SATSetPortScreen();
  89.             SetOrigin(nowOff.h, nowOff.v);
  90.             gSAT.wind.bounds = gSAT.wind.port->portRect; // Synch gSAT.wind.bounds with the portRect!
  91.             srcRect = gSAT.wind.port->portRect;
  92.  
  93.             CopyBits(&gSAT.offScreen.port->portBits, &gSAT.wind.port->portBits, &srcRect, &srcRect, srcCopy, nil);
  94.  
  95.             frameTime = TickCount() - startTicks;
  96.         }
  97.         while ((nowOff.h != where.h) || (nowOff.v != where.v));
  98.     }
  99.  
  100. // I tried to add this.
  101. // Shouldn't matter anymore - ox and oy are obsolete /IR
  102. //        gSAT.ox += nowOff.h-were.h;
  103. //        gSAT.oy += nowOff.v-were.v;
  104. } //ScrollScreen
  105.  
  106. void SetupWind()
  107. {
  108.     Rect zr;
  109.     SysEnvRec wrld;
  110.  
  111.     //• Since SAT hasn't been initialized, we can't use gSAT.colorFlag but 
  112.     //• have to check environs ourselves.
  113.     if ( noErr != SysEnvirons(1, &wrld))
  114.         ;//• ignore errors.
  115.  
  116. //    SetRect(&zr, (512-scrollSizeH)/2, 0,  (512-scrollSizeH)/2 + scrollSizeH, 0 + scrollSizeV);
  117. // no- center on screen instead!
  118.     SetRect(&zr, 0, 0, scrollSizeH, scrollSizeV);
  119.     OffsetRect(&zr, (qd.screenBits.bounds.right - qd.screenBits.bounds.left - scrollSizeH) / 2,
  120.                     (qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - scrollSizeV) / 2);
  121.  
  122.     if ( wrld.hasColorQD )
  123.         gWind = NewCWindow(nil, &zr, "\p", false, plainDBox, (WindowPtr)-1L, false, 0);
  124.     else
  125.         gWind = NewWindow(nil, &zr, "\p", false, plainDBox, (WindowPtr)-1L, false, 0);
  126. } //SetupWind
  127.  
  128. Boolean IsOptionPressed(void)
  129. {
  130.     KeyMap km;
  131.  
  132.     GetKeys(km);
  133.     return (km[1] & 4)!=0;
  134. }
  135.  
  136. Boolean IsPressed(unsigned short k)
  137. {
  138.     unsigned char km[16];
  139.     
  140.     GetKeys((unsigned long *)km);
  141.     return((km[k>>3]>>(k&7))&1);
  142. }
  143.  
  144. main ()
  145. {
  146.     Rect        tempRect;
  147.     EventRecord    e;
  148.     
  149.     SATInitToolbox();
  150.     GetDateTime((unsigned long *)&qd.randSeed);
  151.     
  152.     SATConfigure(true, kVPositionSort, kBackwardCollision, 64);
  153.     SetupWind ();
  154.     
  155.     SetRect(&r, 0, 0, 1000, 484);    //the offscreen size
  156.     SATCustomInit(0, 0, &r, gWind, nil, false, false, false, true, false);
  157.     SATSetSpriteRecSize(sizeof(Sprite));
  158.     
  159.     ShowWindow(gSAT.wind.port);
  160.     SelectWindow(gSAT.wind.port);
  161.     SATHideMBar(gWind);
  162.     
  163.     /* fill the backscreen in a pattren */
  164.     SATSetPortBackScreen(); 
  165.     if (IsOptionPressed())
  166.         thepat = SATGetPat(SATRand(5)+128);    // choose a Pat in Random
  167.     else
  168.         thepat = SATGetPat(128);//128-brown wall, 130-gray wall pattren
  169.     SATPenPat(thepat);
  170.     SetRect(&tempRect, 0, 0, gSAT.offSizeH, gSAT.offSizeV+100);
  171.     PaintRect(&tempRect);
  172.     CopyBits(&(gSAT.backScreen.port)->portBits, &gSAT.offScreen.port->portBits, &gSAT.offScreen.port->portRect, &gSAT.offScreen.port->portRect, srcCopy, nil);
  173.     SATBackChanged(&gSAT.offScreen.port->portRect);
  174.     SATRedraw();
  175.     
  176.     /*Initialize all sprite units*/
  177.     InitPlayerSprite();
  178.     InitPlatform();
  179.     InitEmptyPlatform();
  180.     InitMovPlatform();
  181.     InitHMovPlatform();
  182.     InitInformationArea();
  183.     
  184.     SATRedraw();
  185.     
  186.     /* SetUp my Sprites    */
  187.     GetMouse(&p);
  188.     playerSp = SATNewSprite(1, p.h, p.v, (void *)&SetupPlayerSprite);    // Keep Player Sprite
  189.     ignoreSp = SATNewSprite(0, 0, 350, &SetupEmptyPlatform);    // the Floor Platform
  190.         SetRect(&ignoreSp->hotRect,0,0,gSAT.offSizeH-150,20);
  191.     ignoreSp = SATNewSprite(0, 55, 300, &SetupPlatform);        // Standing Platform
  192.     ignoreSp = SATNewSprite(0, 245, 235, &SetupPlatform);        // Standing Platform
  193.     ignoreSp = SATNewSprite(55, 355, 200, &SetupMovPlatform);    //50=MinV    200=MaxV
  194.         //ignoreSp->position.h=30;    // I can change the defeults
  195.     ignoreSp = SATNewSprite(100, 55, 200, &SetupMovPlatform);    //100=MinV    230=MaxV
  196.     ignoreSp = SATNewSprite(128, 270, 120, &SetupHMovPlatform);    //120=MinH   150=MaxH
  197.         ignoreSp->speed.h = 2;
  198.     
  199.     ignoreSp = SATNewSprite(250, 850, 400, &SetupMovPlatform);    //50=MinV    200=MaxV
  200.  
  201.     ignoreSp = SATNewSprite(55, 700, 250, &SetupMovPlatform);    //50=MinV    200=MaxV
  202.     ignoreSp = SATNewSprite(430, 630, 85, &SetupHMovPlatform);    //50=MinV    200=MaxV
  203.  
  204.     /*Update the game window once more, so the pattern and what we drawn in DrawInfo are shown. */
  205.     SATRedraw();
  206.  
  207.     HideCursor();
  208.     SATSoundOff();
  209.  
  210. #ifndef __MWERKS__
  211.     if (gSAT.colorFlag) // Can't do SlotVInstall without CQD
  212.         if (IsOptionPressed())    {
  213.             InstallVBLCounter();
  214.             SATInstallSynch(&VBLCounterProc);    //WaitForSync
  215.             SysBeep(7);
  216.             gVBLInstalled = true;
  217.         }
  218. #endif
  219.     
  220. /* the main loop */
  221.  
  222.     gSAT.wind.bounds = (*gSAT.wind.port).portRect;
  223.     SATRedraw();
  224.     SATSetPortScreen();
  225.     do
  226.     {
  227.         SATRun(true);    //!IsOptionPressed()
  228.         ScrollScreen();
  229.         if (IsOptionPressed())
  230.             DrawProgrammerInfo();
  231.                         //if (IsOptionPressed()) if (WaitNextEvent ( everyEvent, &e, 10, nil )) ;
  232.     } while (! Button ());
  233.  
  234. /* cleanning up */
  235.     SATSetPortScreen();
  236. #ifndef __MWERKS__
  237.     if (gVBLInstalled)
  238.         RemoveVBLCounter();
  239. #endif
  240.     SATSoundShutup();
  241.     
  242.     ShowCursor();
  243.     FlushEvents(everyEvent, 0);
  244. } //main
  245.